<template>
  <!-- 添加新合同修改 -->
    <el-dialog title="修改" :visible.sync="openContractNewAdd" width="450px" append-to-body :destroy-on-close="true" :before-close="beforeClose">
      <el-form ref="formParams" :model="formParams" :rules="rules" label-width="150px" v-show="showSearch" :inline="true">
        <el-form-item label="合同编号" prop="csbh" :rules="rules.csbh">
          <el-input
            v-model="formParams.csbh"
            placeholder="请输入合同编号"
            clearable
          />
        </el-form-item>
        <el-form-item label="备注" prop="itbz">
          <el-input
            v-model="formParams.itbz"
            placeholder="请输入备注"
            clearable
          />
        </el-form-item>
         <el-form-item label="首次开始日期" prop="sdate" :rules="rules.sdate">
          <el-date-picker
            v-model="formParams.sdate"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择开始日期"
            >
          </el-date-picker>
        </el-form-item>
         <el-form-item label="首次结束日期" prop="edate" :rules="rules.edate">
          <el-date-picker
            v-model="formParams.edate"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择首次结束日期"
            >
          </el-date-picker>
        </el-form-item>
        <el-row class="mt20" type="flex" justify="center">
          <el-col :span="1.5">
            <el-button type="primary" size="mini" @click="contractAddSubmit('formParams')" v-hasPermi="['zhcloud:sfbz:add']">添加合同</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
</template>

<script>
import { addContractFeeStandard  }  from "@/api/storage/contractFeeStandard";
import { calculateDays } from '@/utils'

export default {
  name: "ContractNewAdd",
  data() {
    return {
      showSearch:true,
      //弹窗状态
      openContractNewAdd: false,
      //表单参数
      formParams: {
        khid:"",
        csbh:"",
        itbz:"",
        sdate:"",
        edate:"",
      },
      rules: {
        csbh: { required: true, message: "合同编号不能为空", trigger: "blur" },
        sdate: { required: true, message: "首次开始日期不能为空", trigger: "blur" },
        edate: { required: true, message: "首次结束日期不能为空", trigger: "blur" },
      },
    };
  },
  methods: {
    addNewContract() {
      this.$modal.loading("数据正在添加，请稍等...")
      addContractFeeStandard(this.formParams).then(response => {
        this.$modal.closeLoading()
        this.close()
        this.$emit('refresh', true)
      }).catch(()=> {
        this.$modal.closeLoading()
      })
    },
    /**添加合同 */
    contractAddSubmit(formName) {
      this.$refs[formName].validate((valid, data) => {
        if(valid) {
           const date1 = this.formParams.sdate
          const date2 = this.formParams.edate
          if(date1 && date2) {
            const days = calculateDays(date1, date2)
            if(days < 0) {
              this.$modal.msgError("首次开始日期需小于等于首次结束日期!")
            } else {
              this.$modal.confirm("是否添加合同?").then(() => {
                this.addNewContract()
              }).catch(() => {})
            }
          }
        } else {
          for(let i in data) {
            const item = data[i]
            for(let j in item) {
              const message = item[j].message
              return this.$modal.msgError(message)
            }
          }
          return false
        }
      })
    },

    /**改变窗口状态 */
    changeContractNewAdd() {
      this.openContractNewAdd = !this.openContractNewAdd;
    },
    /**重置数据 */
    reset(vm) {
      Object.assign(vm.$data, vm.$options.data())
    },
    /**关闭弹窗 */
    beforeClose(done) {
      this.close()
      done()
    },
    /**关闭弹窗 */
    close() {
      this.changeContractNewAdd()
      this.reset(this)
    },
    /**打开弹窗 */
    open(data) {
      this.changeContractNewAdd()
      this.formParams.khid = data.khid
    }
  }
};
</script>
